package com.battery.service.impl;

import com.battery.dao.Deposite_OrderDao;
import com.battery.pojo.Deposite_Order;
import com.battery.service.Deposite_OrderService;
import com.battery.util.result.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class Deposite_OrderServiceImpl implements Deposite_OrderService {
    @Autowired(required=false)
    private Deposite_OrderDao deposite_OrderDao;
    @Override
    public Result addDeposite_Order(Deposite_Order deposite_Order) {


        try {
            if(deposite_OrderDao.insertDeposite_Order(deposite_Order)>0)
            {
                return Result.ok().message("生成押金订单成功");
            }
            else{
                return Result.error().message("生成押金订单失败");
            }
        }catch (Exception e){
            return Result.error().message("生成押金订单失败");
        }
    }

    @Override
    public Result dropDeposite_Order(int deposite_id) {
        try {
            if(deposite_OrderDao.deleteDeposite_Order(deposite_id)>0)
            {
                return Result.ok().message("删除押金订单成功");
            }
            else{
                return Result.error().message("删除押金订单失败");
            }
        }catch (Exception e){
            return Result.error().message("删除押金订单失败");
        }
    }

    @Override
    public Result queryDeposite_Order(int deposite_id) {
        try {
            Deposite_Order deposite_Order=deposite_OrderDao.selectDeposite_Order(deposite_id);
            if(deposite_Order!=null)
            {
                return Result.ok().message("查找押金订单成功").data("deposite_Order",deposite_Order);
            }
            else{
                return Result.error().message("查找押金订单失败");
            }
        }catch (Exception e){
            return Result.error().message("查找押金订单失败");
        }
    }

    @Override
    public Result modifyDeposite_Order(Deposite_Order deposite_Order) {
        try{
            if (deposite_OrderDao.updateDeposite_Order(deposite_Order)>0){
                return Result.ok().message("修改成功");
            }
            else return Result.error().message("修改失败：未找到对应记录");
        }catch (DuplicateKeyException e){
            System.err.println(e);
            return Result.error().message("修改失败：已经存在对应记录");
        }
        catch (Exception e){
            System.err.println(e);
            return Result.error().message("修改失败："+e.toString());
        }
    }

    @Override
    public Result queryAllMerDeposite_Order(int mer_id) {
        try {
            List<Deposite_Order> deposite_Orders=deposite_OrderDao.getAllMerDeposite_Order(mer_id);
            if(deposite_Orders.size()>0)
            {
                return Result.ok().message("查找商户所有押金订单成功").data("deposite_Orders",deposite_Orders);
            }
            else{
                return Result.error().message("查找商户所有押金订单失败");
            }
        }catch (Exception e){
            return Result.error().message("查找商户所有押金订单失败");
        }
    }

    @Override
    public Result queryAllUserDeposite_Order(int user_id) {
        try {
            List<Deposite_Order> deposite_Orders=deposite_OrderDao.getAllUserDeposite_Order(user_id);
            if(deposite_Orders.size()>0)
            {
                return Result.ok().message("查找用户所有押金订单成功").data("deposite_Orders",deposite_Orders);
            }
            else{
                return Result.error().message("查找用户所有押金订单失败");
            }
        }catch (Exception e){
            return Result.error().message("查找用户所有押金订单失败");
        }
    }

    @Override
    public Result queryAllDeposite_Order() {
        try {
            List<Deposite_Order> deposite_Orders=deposite_OrderDao.getAllDeposite_Order();
            if(deposite_Orders.size()>0)
            {
                return Result.ok().message("查找所有押金订单成功").data("deposite_Orders",deposite_Orders);
            }
            else{
                return Result.error().message("查找所有押金订单失败");
            }
        }catch (Exception e){
            return Result.error().message("查找所有押金订单失败");
        }
    }

}
